//
//  YHXCalendarView.m
//  GFCalendarDemo
//
//  Created by mac on 2017/4/18.
//  Copyright © 2017年 Mercy. All rights reserved.
//

#import "YHXCalendarView.h"
#import "GFCalendar.h"
#define Screen_Width                [UIScreen mainScreen].bounds.size.width
#define Screen_Height               [UIScreen mainScreen].bounds.size.height

@interface YHXCalendarView (){

    NSInteger               _height;
}

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIButton     *sureButton;
@property (nonatomic, strong) NSString     *yerMothDay;
@property (nonatomic, strong) NSString     *endString;

@end

@implementation YHXCalendarView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{

    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    if (Screen_Width == 375) {
        _height = 5;
    }else if (Screen_Width == 320){
    
        _height = 10;
    }else{
    
        _height = 0;
    }
    //日期选择器
    CGFloat width = Screen_Width - 20;
    CGPoint origin = CGPointMake(10, 20);
    GFCalendarView *calendar = [[GFCalendarView alloc] initWithFrameOrigin:origin width:width];
    calendar.backgroundColor = [UIColor whiteColor];
    calendar.didSelectDayHandler = ^(NSInteger year, NSInteger month, NSInteger day) {
        NSLog(@"%ld,%ld,%ld",(long)year,(long)month,(long)day);//年月日
        _yerMothDay = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)month,(long)day];
    };
    [self addSubview:calendar];

    //时间选择器
    _datePicker = [[UIDatePicker alloc]init];
    _datePicker.frame = CGRectMake(10, calendar.frame.origin.y+calendar.frame.size.height-50 - _height, Screen_Width-20, 200*Radio);
    _datePicker.backgroundColor = [UIColor whiteColor];
    _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
    _datePicker.datePickerMode = UIDatePickerModeTime;
    [_datePicker addTarget:self action:@selector(datePickerDidChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_datePicker];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((Screen_Width-20)/2-20, _datePicker.frame.size.height/2-10, 20, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"时";
    label.textColor = [UIColor colorWithRed:0 / 255.0 green:10 / 255.0 blue:37 / 255.0 alpha:1.0];
    label.font = [UIFont systemFontOfSize:20];
    label.textAlignment = NSTextAlignmentCenter;
    [_datePicker addSubview:label];

    UILabel *labelR = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x+label.frame.size.width+55, _datePicker.frame.size.height/2-10, 20, 20)];
    labelR.backgroundColor = [UIColor clearColor];
    labelR.text = @"分";
    labelR.textColor = [UIColor colorWithRed:0 / 255.0 green:10 / 255.0 blue:37 / 255.0 alpha:1.0];
    labelR.font = [UIFont systemFontOfSize:20];
    labelR.textAlignment = NSTextAlignmentCenter;
    [_datePicker addSubview:labelR];

    _sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _sureButton.frame = CGRectMake(30, _datePicker.frame.origin.y+_datePicker.frame.size.height+10*Radio - _height, Screen_Width-60, 40*Radio);
    _sureButton.backgroundColor = [UIColor colorWithRed:57 / 255.0 green:186 / 255.0 blue:241 / 255.0 alpha:1.0];
    [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureButton setTitle:@"确认" forState:UIControlStateNormal];
    _sureButton.layer.cornerRadius = 5;
    [_sureButton addTarget:self action:@selector(sureTimeClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_sureButton];
}

- (void)datePickerDidChange:(id)sender {
    
    NSDate *date = _datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString  *string = [[NSString alloc]init];
    string = [dateFormatter stringFromDate:date];
    NSLog(@"%@",string);
    if (!_yerMothDay) {
        _yerMothDay = [[NSString stringWithFormat:@"%@",[NSDate date]] substringToIndex:11];
    }
    _endString = [NSString stringWithFormat:@"%@ %@",_yerMothDay,string];
}

- (void)sureTimeClicked{


    if (!_endString && !_yerMothDay) {
        _yerMothDay = [[NSString stringWithFormat:@"%@",[NSDate date]] substringToIndex:11];
        NSDate *date = _datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString  *string = [[NSString alloc]init];
        string = [dateFormatter stringFromDate:date];
        _endString = [NSString stringWithFormat:@"%@ %@",_yerMothDay,string];
    }
    else if (!_endString && _yerMothDay){
    
        NSDate *date = _datePicker.date;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString  *string = [[NSString alloc]init];
        string = [dateFormatter stringFromDate:date];
        _endString = [NSString stringWithFormat:@"%@ %@",_yerMothDay,string];
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"hideCover" object:_endString];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frame = CGRectMake(0, Screen_Height, Screen_Width, 0);
        self.frame = frame;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];

}

@end
